'use strict'
import React, {
    Component
} from 'react'
import {
    Link
} from 'react-router'

import Header from './Common/Header'
import LoadingBox from './Common/LoadingBox'
import {
    HttpService,
    getLocalTime,
    Tips
} from '../Tools'

export default class Notice extends Component {
    constructor(props) {
        super(props);
        this.page = 1;
        this.pagesize = 10;
        this.toBottom = 60;
        this.allDataList = [];
        this.type = 0;
        this.state = {
            noticeList: [],
            dataLoad: 1,
            dataEnd: 0,
            dataNull: 0
        }
    }
    componentWillMount() {
        this.getNotice();
    }
    componentDidMount() {
        window.onscroll = () => {
            const {
                dataLoad,
                dataEnd,
                dataNull
            } = this.state;
            if (dataLoad == 1 || dataEnd == 1 || dataNull == 1)
                return;
                var scrollTop = 0;
                if (document.documentElement && document.documentElement.scrollTop) {
                    scrollTop = document.documentElement.scrollTop;
                } else if (document.body) {
                    scrollTop = document.body.scrollTop;
                }
            if (document.body.clientHeight - window.innerHeight - scrollTop <= this.toBottom) {
                this.page++;
                this.getNotice();
            }
        }
    }

    //类型判断
    getType(index) {
        document.querySelector('#gettype .select').className = ''
        document.getElementById('gettype').children[index].className = 'select'
        this.type = index
        this.setState({
            noticeList: []
        })
        this.getNotice();
    }

    //获取数据列表
    getNotice() {
        this.setState({
            dataLoad: 1,
            dataEnd: 0,
            dataNull: 0
        });
        HttpService.query({
            url: '/jxt-commonbackground/ajaxJybNotice.do',
            data: {
                page: this.page,
                pagesize: this.pagesize,
                jybTicket: sessionStorage.ticket,
                method: 'm101LoadOrgNoticesPage'
            },
            success: (res => {
                this.setState({
                    dataLoad: 0
                });
                if (res.code == 0) {
                    //获取数据成功
                    let newData = this.state.noticeList.concat(res.r.notices);
                    this.setState({
                        noticeList: newData
                    });
                    if (res.r.totalRecordCount == this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                    } else if (res.r.notices.length < this.pagesize) {
                        this.setState({
                            dataEnd: 1
                        });
                        if (newData.length == 0) {
                            this.setState({
                                dataEnd: 0,
                                dataNull: 1
                            });
                        }
                    }
                    return;
                }
                Tips(res.msg);
            })
        })
    }
    render() {
        const {
            dataLoad,
            dataEnd,
            dataNull,
            noticeList
        } = this.state
        const noticeTagList = this.state.noticeList.map((item, index) => {
            const timeStr = getLocalTime(item.createDate)
            const newIdea = item.read == 0 || (item.requireSign == 1 && item.sign == 0)
            // if (this.type == 0 && !(readno)) { //未读
            //     return
            // }
            return (
                <Link className="pnt_item" to={'/NtcDetail/' + item.id}>
                    <span className="type"></span>
                    <h5><span>{item.title}</span>{newIdea
                            ? <i>new</i>
                            : ''}
                    </h5>
                    {/*<div className="intro">请所有家长于2017年7月来学校参加家长。</div>*/}
                    <div className="time">
                        {timeStr}
                    </div>
                    {/*<span className='ready'>{item.readCount}人已读</span>*/}
                </Link>
            )
        })
        return (
            <div>
                <Header title='校内公告' appLink='1'/>
                <div className="pjb_noticelist">
                    {/*<div className="type_list" id='gettype'>
                        <div className="select" onClick={this.getType.bind(this, 0)}>未读</div>
                        <div onClick={this.getType.bind(this, 1)}>已读</div>
                    </div>*/}
                    {noticeTagList}
                </div>
                <LoadingBox dataLoad={dataLoad} dataNull={dataNull} dataEnd={dataEnd}/>
            </div>
        )
    }
}
